import {SysConfig} from '@/libs/config'

export class TokenUtil {
	/**
   * @description 获取token

   */

	static getToken() {
		const { tokenTableName, tokenStorage } = SysConfig
		if (tokenStorage) {
			if (tokenStorage === 'LOCAL') return localStorage.getItem(tokenTableName)
			if (tokenStorage === 'SESSION') return sessionStorage.getItem(tokenTableName)
			return localStorage.getItem(tokenTableName)
		}
		return localStorage.getItem(tokenTableName)
	}

	/**
	 * @description 存储token
	 * @param token
	 * @returns {void|*}
	 */
	static setToken(token) {
		const { tokenTableName, tokenStorage } = SysConfig
		if (tokenStorage) {
			if (tokenStorage === 'LOCAL') return localStorage.setItem(tokenTableName, token)
			if (tokenStorage === 'SESSION') return sessionStorage.setItem(tokenTableName, token)
			return localStorage.setItem(tokenTableName, token)
		}
		return localStorage.setItem(tokenTableName, token)
	}

	/**
	 * @description 移除token
	 * @returns {void|Promise<void>}
	 */
	static removeToken() {
		const { tokenTableName, tokenStorage } = SysConfig
		if (tokenStorage) {
			if (tokenStorage === 'LOCAL') return localStorage.removeItem(tokenTableName)
			if (tokenStorage === 'SESSION') return sessionStorage.clear()
			return localStorage.removeItem(tokenTableName)
		}
		return localStorage.removeItem(tokenTableName)
	}
}
